package com.itboye.ihomebank.adapter;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.QianYueManageBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.home.ActivityEditHeTongFangdong;
import com.itboye.ihomebank.home.ActivityEditHeTongZuKe;
import com.itboye.ihomebank.home.ActivityHuaBan;
import com.itboye.ihomebank.home.TianXieHeTongActivityFangDong;
import com.itboye.ihomebank.home.TianXieHeTongActivityZuKe;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;
import com.itboye.ihomebank.web.WebActivity;

/***
 * 签约管理
 * 
 * @author dsawe
 * 
 */
public class QianYueAdapter extends MutiplyCommonAdapter<QianYueManageBean>
		implements Observer {
	private List<QianYueManageBean> orderQueryModel;
	Activity context;
	private int type;
	protected String uid;
	protected String password;
	UserPresenter userPresenter;
	protected String tempContractNo;

	public QianYueAdapter(Activity context, List<QianYueManageBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		this.orderQueryModel = datas;
		this.context = context;
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
		password = SPUtils.get(context, null, SPContants.PASSWORD, "") + "";
		userPresenter = new UserPresenter(this);
		orderQueryModel = datas;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.queRenHeTong_success) {
				ByAlert.alert(entity.getData());
				// Iterator<QianYueManageBean> iterator = orderQueryModel
				// .iterator();
				// while (iterator.hasNext()) {
				// QianYueManageBean bean = iterator.next();
				// if (bean.getContractNo().equals(tempContractNo)) {
				// iterator.remove();
				// notifyDataSetChanged();
				// break;
				// }
				// }
			} else {
				ByAlert.alert(entity.getData());
			}
		}
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void convert(ViewHolder holder, final QianYueManageBean t, int type,
			int position) {
		int fakeStatus = Integer.parseInt(t.getRenterWritten());
		int fangDongStatus = Integer.parseInt(t.getLesserWritten());
		String shenfen = t.getUserType();
		if (shenfen.equals("lesser")) {// 房东
			if (fakeStatus == 0 && fangDongStatus == 0) {
				holder.setText(R.id.all_left, "填写合同");
				holder.setText(R.id.all_right, "查看合同");
				holder.setVisible(R.id.all_center, false);
			} else if (fakeStatus == 0 && fangDongStatus == 1) {
				holder.setText(R.id.all_left, "修改合同");
				holder.setText(R.id.all_right, "查看合同");
				holder.setVisible(R.id.all_center, false);
			} else if (fakeStatus == 1 && fangDongStatus == 1) {
				holder.setText(R.id.all_center, "确认完成");
				holder.setText(R.id.all_left, "修改合同");
				holder.setText(R.id.all_right, "查看合同");
				holder.setVisible(R.id.all_center, true);
			}
		} else {// 租客
			if (fakeStatus == 0 && fangDongStatus == 0) {
				holder.setVisible(R.id.all_left, false);
				holder.setVisible(R.id.all_center, false);
				holder.setText(R.id.all_right, "查看合同");
			} else if (fakeStatus == 0 && fangDongStatus == 1) {
				holder.setVisible(R.id.all_left, true);
				holder.setText(R.id.all_left, "签订合同");
				holder.setText(R.id.all_right, "查看合同");
				holder.setVisible(R.id.all_center, false);
			} else if (fakeStatus == 1 && fangDongStatus == 1) {
				holder.setVisible(R.id.all_left, true);
				holder.setText(R.id.all_center, "确认完成");
				holder.setText(R.id.all_left, "重新签名");
				holder.setText(R.id.all_right, "查看合同");
				holder.setVisible(R.id.all_center, true);
			}
		}
		holder.setText(R.id.txt_name, t.getTitle());
		holder.setText(R.id.tv_shuxing,t.getHouseRoom() + "室/" + t.getHouseHall() + "厅/"
						+ t.getHouseArea() + "㎡");
		holder.setVisible(R.id.txt_zhuangtai, false);
		holder.setText(R.id.hetong, "合同号:" + t.getContractNo());

			XImageLoader.load(NetPublicConstant.IMAGE_URL + t.getImgId(),
					(ImageView) holder.getView(R.id.img_house));
		// holder.setText(R.id.txt_name, t.getTitle());
		// if (type == 0) {
		// holder.setText(R.id.txt_zhuangtai, "待签约");
		// } else {
		// holder.setText(R.id.txt_zhuangtai, "已完成");
		// }
		holder.setText(R.id.txt_zujin, Double.parseDouble(t.getRent()) / 1000
				+ "元/月");
		holder.setOnClickListener(R.id.all_left, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String shenfen = t.getUserType();
						TextView hetong = (TextView) v;
						String text = hetong.getText().toString();
						if (text.equals("填写合同")) {
							if (!shenfen.equals("renter")) {// 租客
								Intent intent = new Intent(context,
										TianXieHeTongActivityFangDong.class);
								intent.putExtra("contractNo", t.getContractNo());
								
								context.startActivity(intent);
							} else {
//								Intent intent = new Intent(context,
//										TianXieHeTongActivityZuKe.class);
//								intent.putExtra("contractNo", t.getContractNo());
								Intent intent = new Intent(//直接跳转至画板
										context,
										ActivityHuaBan.class);
//								startActivityForResult(intent, 107);//
								context.startActivity(intent);
							}
						} else if (text.equals("修改合同")) {
							if (shenfen.equals("renter")) {// 租客
//								Intent intent = new Intent(context,
//										ActivityEditHeTongZuKe.class);
//								intent.putExtra("contractNo", t.getContractNo());
//								intent.putExtra("shenfen", "renter");
//								context.startActivity(intent);
								Intent intent = new Intent(//直接跳转至画板
										context,
										ActivityHuaBan.class);
								intent.putExtra("contractNo", t.getContractNo());
//								startActivityForResult(intent, 107);//
								context.startActivity(intent);

							} else {
								Intent intent = new Intent(context,
										ActivityEditHeTongFangdong.class);
								intent.putExtra("contractNo", t.getContractNo());
								intent.putExtra("shenfen", "lesser");
								context.startActivity(intent);
							}
						}else if(text.equals("签订合同")){
							Intent intent = new Intent(//直接跳转至画板
									context,
									ActivityHuaBan.class);
							intent.putExtra("contractNo", t.getContractNo());
//							startActivityForResult(intent, 107);//
							context.startActivity(intent);
						}
					}
				});
		holder.setOnClickListener(R.id.all_right, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(context, WebActivity.class);
						intent.putExtra("url", NetPublicConstant.HETONG_URL
								+ "?uid=" + uid + "&psw=" + password
								+ "&contract_no=" + t.getContractNo()+"&pc=1");
						intent.putExtra("title", "查看合同");
						context.startActivity(intent);
					}
				});
		holder.setOnClickListener(R.id.all_center, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						tempContractNo = t.getContractNo();
						userPresenter.wanChengHeTong(uid, t.getContractNo());
						// Intent intent = new Intent(context,
						// WebActivity.class);
						// intent.putExtra("url", NetPublicConstant.HETONG_URL
						// + "?uid=" + uid + "&psw=" + password
						// + "&contract_no=" + t.getContractNo());
						// context.startActivity(intent);
					}
				});
	}

	public void setStatus(int type) {
		this.type = type;
	}
}
